package com.rui.framework.beans;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author : [锐神NO.3]
 * @version : [v1.0]
 * @className : MutablePropertyValues
 * @createTime : [2022/2/26 15:18]
 * @description : [用来存储和管理多个propertyValue对象]
 */

public class MutablePropertyValues implements Iterable<PropertyValue> {

    /**
     * 定义list集合对象，用来存储PropertyValue对象
     */
    private final List<PropertyValue> propertyValueList;

    public MutablePropertyValues() {
        this.propertyValueList = new ArrayList<>();
    }

    public MutablePropertyValues(List<PropertyValue> propertyValueList) {
        if(propertyValueList == null) {
            this.propertyValueList = new ArrayList<>();
        }else {
            this.propertyValueList = propertyValueList;
        }
    }

    /**
     * 获取所有的PropertyValue对象，返回以数组的形式
     */
    public PropertyValue[] getPropertyValues() {
        return this.propertyValueList.toArray(new PropertyValue[0]);
    }

    /**
     * 根据name属性值获取PropertyValue对象
     */
    public PropertyValue getPropertyValue(String propertyName) {
        for (PropertyValue propertyValue : propertyValueList) {
            if(propertyValue.getName().equals(propertyName)) {
                return propertyValue;
            }
        }
        return null;
    }

    /**
     * 判断集合是否为空
     */
    public boolean isEmpty() {
        return this.propertyValueList.isEmpty();
    }

    /**
     * 添加PropertyValue对象,并且实现链式编程
     */
    public MutablePropertyValues addPropertyValue(PropertyValue propertyValue) {
        //判断集合中存储的propertyValue对象是否和传递的propertyValue对象相同，如果相同，则覆盖
        for (int i = 0; i < propertyValueList.size(); i++) {
            //获取集合中的每一个PropertyValue对象
            PropertyValue currentPropertyValue = propertyValueList.get(i);
            if(currentPropertyValue.getName().equals(propertyValue.getName())) {
                propertyValueList.set(i, propertyValue);
                return this;
            }
        }
        this.propertyValueList.add(propertyValue);
        return this;
    }

    /**
     * 判断是否有指定name属性值的对象
     */
    public boolean contains(String propertyName) {
        return getPropertyValue(propertyName) != null;
    }

    /**
     * Returns an iterator over elements of type {@code T}.
     *
     * @return an Iterator.
     */
    @Override
    public Iterator<PropertyValue> iterator() {
        return null;
    }
}
